package com.housediyadmin.view.components
{
	/**
	 * 
	 * @author Bo Zhang
	 * 2011/01/07
	 */	
	import com.adobe.cairngorm.popup.PopUpEvent;
	import com.adobe.cairngorm.popup.PopUpWrapper;
	import com.housediyadmin.events.RemoveGoodsEvent;
	import com.housediyadmin.events.bubbleEvents.CheckSalesIDBubbleEvt;
	import com.housediyadmin.events.goodsEvents.GetGoodsBySalesIDEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByTypeIDEvent;
	import com.housediyadmin.events.salesEvents.AddSalesEvent;
	import com.housediyadmin.events.salesEvents.DeleteSalesEvent;
	import com.housediyadmin.events.salesEvents.EditSalesEvent;
	import com.housediyadmin.events.salesEvents.GetSalesEvent;
	import com.housediyadmin.events.treeEvents.GetGoodsTreeEvent;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.GoodsTreeModel;
	import com.housediyadmin.model.SalesModel;
	import com.housediyadmin.vo.Goods;
	import com.housediyadmin.vo.Sales;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.core.DragSource;
	import mx.core.IUIComponent;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.events.DragEvent;
	import mx.events.ListEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Label;
	import spark.components.List;
	
	public class SalesPopUpMediator extends Mediator
	{
		[Inject]
		public var view:SalesPopUp;
		
		[Inject]
		public var model:GoodsModel;
		
		[Inject]
		public var salesModel:SalesModel;
				
		[Inject]
		public var goodsTreeModel:GoodsTreeModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
					
		override public function onRegister():void
		{
			var dateFormatter:DateFormatter = new DateFormatter();
			dateFormatter.formatString = "YYYY年MM月DD日";
			
			BindingUtils.bindProperty(view.goodsTree, "dataProvider", goodsTreeModel, "goodsTreeXML");
			dispatch(new GetGoodsTreeEvent(GetGoodsTreeEvent.GET_GOODS_TREE));
			view.goodsTree.addEventListener(ListEvent.ITEM_CLICK, selectTreeNode);
			
			BindingUtils.bindProperty(view.allGoodslist,"dataProvider", goodsModel, "goodsArray");
			BindingUtils.bindProperty(view.selectedGoodsList,"dataProvider", goodsModel, "goodsSalesArray");
			
			if(view.currentState == "add")
			{
				goodsModel.goodsArray = new ArrayCollection();		
				goodsModel.goodsSalesArray = new ArrayCollection();
				
				view.salesCreateTime.text = dateFormatter.format(new Date());
				view.salesEditorTime.text = dateFormatter.format(new Date());
				view.salesStartTime.text = dateFormatter.format(new Date());
				view.salesEndTime.text = dateFormatter.format(new Date());
				view.salesCreateTime.enabled = false;
				view.salesEditorTime.enabled = false;
				salesModel.selectedSales.salesStartDate = new Date();
				salesModel.selectedSales.salesEndDate = new Date();
				view.salesStartTime.disabledRanges = [{rangeEnd:new Date()}]; 
				view.salesEndTime.disabledRanges = [{rangeEnd:salesModel.selectedSales.salesStartDate}]; 
				view.salesStartTime.addEventListener(CalendarLayoutChangeEvent.CHANGE, salesStartTime); 
				view.salesEndTime.addEventListener(CalendarLayoutChangeEvent.CHANGE, salesEndTime); 
				
				view.okBtn.addEventListener(MouseEvent.CLICK, addSalesEve);
			}
			else if(view.currentState == "edit")
			{
				goodsModel.goodsArray = new ArrayCollection();		
				BindingUtils.bindProperty(view.salesTitle, "text", salesModel.selectedSales, "salesTitle");
				BindingUtils.bindProperty(view.salesContent, "text", salesModel.selectedSales, "salesContent");
				BindingUtils.bindProperty(view.salesPrice, "text", salesModel.selectedSales, "salesPrice");
				BindingUtils.bindProperty(view.salesCreator, "text", salesModel.selectedSales, "salesCreator");
				BindingUtils.bindProperty(view.salesEditor, "text", salesModel.selectedSales, "salesEditor");
				
				var date:Date = salesModel.selectedSales.salesCreateDate;
				view.salesCreateTime.text = dateFormatter.format(date);	
				view.salesStartTime.text = dateFormatter.format(salesModel.selectedSales.salesStartDate);
				view.salesEndTime.text = dateFormatter.format(salesModel.selectedSales.salesEndDate);
				view.salesEditorTime.text = dateFormatter.format(salesModel.selectedSales.salesEditDate);
				view.salesCreateTime.enabled = false;
				view.salesEditorTime.enabled = false;
				view.salesCreator.enabled = false;
				view.salesEndTime.disabledRanges = [{rangeEnd:salesModel.selectedSales.salesStartDate}]; 
				view.salesStartTime.addEventListener(CalendarLayoutChangeEvent.CHANGE, salesStartTime); 
				view.salesEndTime.addEventListener(CalendarLayoutChangeEvent.CHANGE, salesEndTime); 
				dispatch(new GetGoodsBySalesIDEvent(GetGoodsBySalesIDEvent.GET_GOODS_BY_SALESID, salesModel.selectedSales.salesID));
				
				view.okBtn.addEventListener(MouseEvent.CLICK, modifySalesEve);
			}
			
			view.allGoodslist.dataProvider = model.goodsArray;
			view.selectedGoodsList.dataProvider = model.goodsSalesArray;
			view.allGoodslist.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);			
			view.addEventListener(RemoveGoodsEvent.REMOVE_GOODS, removeGoodsHandler);
			view.selectedGoodsList.addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler);		
			view.selectedGoodsList.addEventListener(DragEvent.DRAG_DROP, dragDropHandler);	
			view.cancelBtn.addEventListener(MouseEvent.CLICK, cancelEve);	
		}
		
		private function startDragHandler(event:MouseEvent):void
		{	
			if (view.allGoodslist.selectedItem != null)
			{
				var ds:DragSource = new DragSource();
				ds.addData((view.allGoodslist.selectedItem as Goods), "moveGoods");
				
				var imageProxy:Label = new Label();
				imageProxy.text = (view.allGoodslist.selectedItem as Goods).goodsName;
				
				DragManager.doDrag(view.allGoodslist, ds, event, imageProxy);	
			}
			else
			{
				Alert.show("请选择需要添加的商品");
			}				
		}
		
		private function removeGoodsHandler(event:RemoveGoodsEvent):void
		{
			model.goodsSalesArray.removeItemAt(model.goodsSalesArray.getItemIndex(event.goods));
		}
		
		private function dragEnterHandler(event:DragEvent):void
		{
			var selectedGoods:Goods = event.dragSource.dataForFormat(event.dragSource.formats[0]) as Goods;		
			var couldDrop:Boolean = true;
			
			for each (var goods:Goods in model.goodsSalesArray)
			{
				if (goods.goodsName == selectedGoods.goodsName)
				{
					couldDrop = false;
					break;		
				}
			}
			if (couldDrop == true && event.dragSource.hasFormat("moveGoods"))
			{
				DragManager.acceptDragDrop(IUIComponent(event.target));
			}		
		}
		
		private function dragDropHandler(event:DragEvent):void
		{
			model.goodsSalesArray.addItem(event.dragSource.dataForFormat(event.dragSource.formats[0]) as Goods);
		}
		
		private function addSalesEve(event:MouseEvent):void
		{
			salesModel.selectedSales.salesContent = view.salesContent.text;
			salesModel.selectedSales.salesCreator = view.salesCreator.text;
			salesModel.selectedSales.salesEditor = view.salesEditor.text;
			salesModel.selectedSales.salesPrice = (int)(view.salesPrice.text);
			salesModel.selectedSales.salesTitle = view.salesTitle.text;		
			salesModel.selectedSales.salesCreateDate = new Date();
			salesModel.selectedSales.salesEditDate = new Date();		
			salesModel.selectedSales.salesStartDate = salesModel.selectedSales.salesStartDate;
			salesModel.selectedSales.salesEndDate = salesModel.selectedSales.salesEndDate;
			
			var goodsIds:Array = new Array();
			for(var i:int = 0; i < goodsModel.goodsSalesArray.length; i++)
			{
				goodsIds.push((goodsModel.goodsSalesArray[i] as Goods).goodsID);
			}
			dispatch(new AddSalesEvent(AddSalesEvent.ADD_SALES, salesModel.selectedSales, goodsIds));
			
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			salesModel.selectedSales = new Sales();
		}
		
		private function cancelEve(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			salesModel.selectedSales = new Sales();
		}
		
		private function modifySalesEve(event:MouseEvent):void
		{
			
			salesModel.selectedSales.salesContent = view.salesContent.text;
			salesModel.selectedSales.salesTitle = view.salesTitle.text;
			salesModel.selectedSales.salesPrice = (int)(view.salesPrice.text);
			salesModel.selectedSales.salesCreator = salesModel.selectedSales.salesCreator;
			salesModel.selectedSales.salesEditor = view.salesEditor.text;
			salesModel.selectedSales.salesCreateDate = salesModel.selectedSales.salesCreateDate;
			
			salesModel.selectedSales.salesStartDate = salesModel.selectedSales.salesStartDate;
			salesModel.selectedSales.salesEndDate = salesModel.selectedSales.salesEndDate;
			salesModel.selectedSales.salesEditDate = new Date();
			
			var goodsIds:Array = new Array();
			
			for(var i:int; i < goodsModel.goodsSalesArray.length; i++)
			{
				goodsIds.push((goodsModel.goodsSalesArray[i] as Goods).goodsID);
			}
			
			dispatch(new EditSalesEvent(EditSalesEvent.EDIT_SALES, salesModel.selectedSales, goodsIds));
			
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
			salesModel.selectedSales = new Sales();
		}
		
		private function selectTreeNode(event:ListEvent):void
		{
			goodsTreeModel.selectedNode = event.target.selectedItem as XML;	
			
			var goodsIDs:Array = new Array;
			
			var xmlList:XMLList = event.target.selectedItem.@typeID;
			
			if(xmlList.length() != 0)
			{
				for each(var xml:XML in xmlList)
				{
					goodsIDs.push((int)(xml.toXMLString()));
				}
				dispatch(new GetGoodsByTypeIDEvent(GetGoodsByTypeIDEvent.GET_GOODS_BY_TYPEID, goodsIDs));
			}	
		}
		
		private function salesStartTime(event:CalendarLayoutChangeEvent):void
		{
			if(DateField(event.target).selectedDate != null)
			{
				var dateFormatter:DateFormatter = new DateFormatter();    
				dateFormatter.formatString = "YYYY年MM月DD日";   
				
				var selectedDate:Date = event.target.selectedDate;
				selectedDate.hours += 8;
				salesModel.selectedSales.salesStartDate = selectedDate;
				view.salesStartTime.text = dateFormatter.format(selectedDate);
				view.salesEndTime.disabledRanges = [{rangeEnd:salesModel.selectedSales.salesStartDate}]; 
			}
		}
		
		private function salesEndTime(event:CalendarLayoutChangeEvent):void
		{
			if(DateField(event.target).selectedDate != null)
			{
				var dateFormatter:DateFormatter = new DateFormatter();    
				dateFormatter.formatString = "YYYY年MM月DD日";   
				
				var selectedDate:Date = event.target.selectedDate;
				selectedDate.hours += 8;
				salesModel.selectedSales.salesEndDate = selectedDate;
				view.salesEndTime.text = dateFormatter.format(selectedDate);
			}
		}	
	}
}